package com.muy.redis.view.component;

import com.intellij.icons.AllIcons;
import com.intellij.ui.components.JBLabel;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.function.Consumer;

/**
 * @Author jiyanghuang
 * @Date 2023/9/26 22:32
 */
public class MrRedisPagingPanel extends JPanel {

    private JBLabel pageSizeLabel;
    private JBLabel pageLabel;
    private int pageIndex = 1;

    private final int pageSize = 20;

    private JButton prevPageButton;
    private JButton nextPageButton;

    private long total = 0L;

    private int currentPageCount = 0;

    public MrRedisPagingPanel(Consumer<MrRedisPagingPanel> consumer) {
        setLayout(new BorderLayout());
        JPanel pageTop = new JPanel(new BorderLayout());
        pageSizeLabel = new JBLabel("Page Size: " + currentPageCount);
        pageSizeLabel.setBorder(new EmptyBorder(0, 5, 0, 0));

        pageLabel = new JBLabel(String.format("Page %s of %s", pageIndex, getPageCount()));


        MrJSplitPane jSplitPane = new MrJSplitPane(pageSizeLabel, pageLabel);
        add(jSplitPane, BorderLayout.NORTH);

        prevPageButton = new JButton(AllIcons.Actions.Play_back);
        nextPageButton = new JButton(AllIcons.Actions.Play_forward);
        MrJSplitPane pagePanel = new MrJSplitPane(prevPageButton, nextPageButton);
        add(pagePanel, BorderLayout.SOUTH);

        if (pageIndex <= 1) {
            prevPageButton.setEnabled(false);
        }
        prevPageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageIndex <= 1) {
                    return;
                }

                updatePageDataOnPrevButtonClicked();
//                renderKeyTree(parent.getKeyFilter(), parent.getGroupSymbol());
                consumer.accept(MrRedisPagingPanel.this);
                nextPageButton.setEnabled(true);

                if (pageIndex <= 1) {
                    prevPageButton.setEnabled(false);
                }
            }
        });

        if (pageIndex >= getPageCount()) {
            nextPageButton.setEnabled(false);
        }
        nextPageButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageIndex >= getPageCount()) {
                    return;
                }

                updatePageDataOnNextButtonClicked();
//                renderKeyTree(parent.getKeyFilter(), parent.getGroupSymbol());
                consumer.accept(MrRedisPagingPanel.this);
                prevPageButton.setEnabled(true);
                if (pageIndex >= getPageCount()) {
                    nextPageButton.setEnabled(false);
                }
            }
        });
    }

    private synchronized void updatePageDataOnPrevButtonClicked() {
        pageIndex--;
    }

    private synchronized void updatePageDataOnNextButtonClicked() {
        pageIndex++;
    }

    public void resetPageIndex() {
        pageIndex = 1;
    }

    public void updatePageLabel() {
        pageLabel.setText(String.format("Page %s of %s", pageIndex, getPageCount()));
        pageSizeLabel.setText("Page Size: " + currentPageCount);
        nextPageButton.setEnabled(true);
        if (pageIndex >= getPageCount()) {
            nextPageButton.setEnabled(false);
        } else {
            if(currentPageCount < pageSize){
                nextPageButton.setEnabled(false);
            }
        }
        prevPageButton.setEnabled(true);
        if (pageIndex <= 1) {
            prevPageButton.setEnabled(false);
        }
    }


    /**
     * @return
     */
    private long getPageCount() {
        long result = total / pageSize;
        long mod = total % pageSize;
        return mod > 0 ? result + 1 : result;
    }

    public void updateTotal(long total) {
        this.total = total;
    }

    public void updateCurrentPageCount(int currentPageCount) {
        this.currentPageCount = currentPageCount;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int nextStart() {
        return (pageIndex - 1) * pageSize;
    }

}
